Vega-Lite文档: 02_data

Offical documentation

数据集data

  • 与Vega一致, 默认的Vega-Lite中的数据是列表

  • 可以是嵌入的数据值values, 也可以是外链数据url, 还可以是命名数据源name用来绑定上层数据集

  • Vega-Lite还提供简单的数据生成方法

嵌入数据values

PTD
valuesArray数据向量
nameString名字
formatDataFormat数据格式

嵌入数据的方式:

// 1. 最常规: 指定表头=>值映射关系的二维数组:
"data": {
    "values": [
      {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
      {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
      {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
    ]
},

// 2. 简单的数组会被自动展开, 添加默认表头信息
[5, 3, 8, 1] //会被加载成:
[{"data": 5}, {"data": 3}, {"data": 8}, {"data": 1}]

// 3. 输入字符串和指定的预设格式类型
"data": {
    "values": "a\n1\n2\n3\n4",
    "format": {
        "type": "csv"
    }
},

外链数据url

支持url, name, format属性。

默认formatjson

命名数据name

这个平时很少用到, 暂略。

数据格式format

format支持如下属性:

PTD
typeStringjson, csv, tsv, dsv, 默认json
parseObject/Null指定数据中某一列解析成某种格式: number, boolean, date, 如: "parse":{"col1": "date: '%m%d%Y'"}

dsv: delimited text file, 分隔符必须是单个字符

数据生成器

sequence

根据起止和步长生成系列连续数值, 默认情况下数据的列名为data, 可以用as参数更改列名

PropertyTypeDescription
startNumberRequired. The starting value of the sequence (inclusive).
stopNumberRequired. The ending value of the sequence (exclusive).
stepNumberThe step value between sequence entries.Default value: 1
asStringThe name of the generated sequence field.Default value: "data"

{
    "width": 300,
    "height": 150,
    "data": {"sequence": {
             "start": 0, "stop": 12.7, 
             "step": 0.1, "as": "x"}},
    "transform": [{"calculate": "sin(datum.x)", "as": "sin(x)"}],
    "mark": "line",
    "encoding": {
        "x": {"field": "x", "type": "quantitative"},
        "y": {"field": "sin(x)", "type": "quantitative"}
    }
}

graticule

根据经纬度生成地图数据GeoJSON:

PropertyTypeDescription
extentArraySets both the major and minor extents to the same values.
extentMajorArrayThe major extent of the graticule as a two-element array of coordinates.
extentMinorArrayThe minor extent of the graticule as a two-element array of coordinates.
precisionNumberThe precision of the graticule in degrees.Default value: 2.5
stepArraySets both the major and minor step angles to the same values.
stepMajorArrayThe major step angles of the graticule.Default value: [90, 360]
stepMinorArrayThe minor step angles of the graticule.Default value: [10, 10]

{
    "width": 400,
    "height": 400,
    "projection": {
        "type": "orthographic",
        "scale": 100,
        "translate": [100,100]
    },
    "layer": [
        {
        "data": {"sphere": true},
        "mark": {
            "type": "geoshape",
            "fill": "aliceblue"
        }
        },
        {
        "data": {"graticule": true},
        "mark": {
            "type": "geoshape",
            "stroke": "black",
            "strokeWidth": 0.5
        }
        }
    ]
}